﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using medicine.BaseComponents.Groups;
using System.Windows.Forms;
using EventLogger;
using medicine.DAL;

namespace medicine.General
{
    public static class MedicinUtilities
    {
        public static string EXEFilePath = null;
        public static void InitilizeApplication(string lEXEFilePath)
        {
            EXEFilePath = Path.GetDirectoryName(lEXEFilePath);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            MedicineDBStructure.StudyDate = MedicineDBStructure.GetStudyDate();
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            
            cEventLogger.AddEntry(cEventLogger.EventsType.Error, "Exception Occure", e.Exception.Message, e.Exception.TargetSite.Name , e.Exception);
            if (DialogResult.No == MessageBox.Show("Error Occure (" + e.Exception.Message + ").\r\nDo you want to continue?", "Error Occure", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2))
                Application.Exit();
        }

        public delegate void AfterDropCompleteHandler(Mover Sender, Control Parent, Control NewParent);
        public static event AfterDropCompleteHandler AfterDropComplete;
        public delegate void AfterRowSelectionHandler(Control Sender);
        public static event AfterRowSelectionHandler AfterRowSelection;

        public static void AddGroupsLabels(GroupLabel item)
        {
            GroupsLabels.Add(item);
            item.Mover.AfterDropComplete += new BaseComponents.Groups.AfterDropCompleteHandler(Mover_AfterDropComplete);
            item.AfterRowSelection += new GroupLabel.AfterRowSelectionHandler(item_AfterRowSelection);
        }
        public static void ClearGroupsLabels()
        {
            foreach (GroupLabel  item in GroupsLabels)
            {
                item.Dispose();
            }
            GroupsLabels.Clear();
        }

        public static void AddColumnLabels(GroupLabel item)
        {
            ColumnLabels.Add(item);            
            item.AfterRowSelection += new GroupLabel.AfterRowSelectionHandler(item_AfterRowSelection);
        }

        static void item_AfterRowSelection(Control Sender)
        {
            if (AfterRowSelection != null)
                AfterRowSelection(Sender);

        }

        static void Mover_AfterDropComplete(Mover Sender, Control Parent, Control NewParent)
        {
            if (AfterDropComplete != null)
                AfterDropComplete(Sender, Parent, NewParent);
        }

        public static List<GroupLabel> GroupsLabels = new List<GroupLabel>();
        public static List<GroupLabel> ColumnLabels = new List<GroupLabel>();
        
        public static bool IsAllColumnsSelected()
        {
            foreach (GroupLabel item in ColumnLabels)
            {
                if (!item.GroupInfo.isFiltered)
                    return false;
            }
            return true;
        }
        public static GroupLabel GetSpecificColumn(string DBName)
        {
            foreach (GroupLabel item in ColumnLabels)
            {
                if (item.GroupInfo.DBTableName==DBName )
                    return item;
            }
            return null;

        }
        public static GroupLabel GetSpecificGroup(string Text)
        {
            foreach (GroupLabel item in GroupsLabels)
            {
                if (item.GroupInfo.Text  == Text)
                    return item;
            }
            return null;
        }

        public static List<GroupLabel> GetSelectedRowsGroups()
        {
            List<GroupLabel> Result = new List<GroupLabel>();
            foreach (GroupLabel item in GroupsLabels)
                if (item.GroupInfo.isSelectedRow)
                {
                    bool found = false;
                    for (int i = 0; i < Result.Count ; i++)
                    {
                        if (Result[i].GroupInfo.SelectedRowOrder > item.GroupInfo.SelectedRowOrder)
                        {
                            Result.Insert(i, item);
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        Result.Add(item);
                }
            return Result;

        }

        public static StringCollection GetSelectedRowsGroupsTablesNames()
        {
            StringCollection Result = new StringCollection();
            foreach (GroupLabel item in MedicinUtilities.GetSelectedRowsGroups())
            {
                Result.Add(item.GroupInfo.DBTableName);
            }
            return Result;
        }

        public static List<GroupLabel> GetFilteredGroups()
        {
            List<GroupLabel> Result = new List<GroupLabel>();
            foreach (GroupLabel  item in GroupsLabels)
                if (item.GroupInfo.isFiltered)
                    Result.Add(item);
            return Result;
        }

        public static string GetSQLFilter()
        {
            return GetSQLFilter("");
        }
        public static string GetSQLFilter(string ExecludeText)
        {
            string Result = "";

            List<GroupLabel> FilteredGroups = GetFilteredGroups();

            bool isFirst1 = true;
            bool isFirst2 = true;
            foreach (GroupLabel item in FilteredGroups)
            {
                if (!string.IsNullOrWhiteSpace(ExecludeText) && item.GroupInfo.Text  == ExecludeText)
                    continue;
                if (!isFirst1)
                    Result += " AND ";
                isFirst1 = false;
                Result += " ( ";
                isFirst2 = true;
                foreach (string item2 in item.GroupInfo.Selected )
                {
                    //if (!string.IsNullOrEmpty(Result))
                    if (isFirst2)
                        //Result += " OR ";
                        Result += "\r\n" + item.GroupInfo.DBTableName + "." + item.GroupInfo.DBValueField + " IN (";
                    else 
                        Result += " , ";
                    isFirst2 = false;
                   
                    if (item.GroupInfo.DBValueFieldType.ToLower()=="string")
                        Result += "  '" + item2 + "' ";
                    else
                        Result += " " + item2 + " ";
                }
                Result += " )) ";
            }

            return Result;
        }

        public static string GetPeriodsSQLFilter()
        {
            string Result = "";
            foreach (string item in MedicinUtilities.GetSpecificColumn("Periods").GroupInfo.Selected)
            {
                if (!string.IsNullOrEmpty(Result))
                    Result += " OR ";
                PeriodClass CurrentPeriod=PeriodClass.DeSerialize(item);
                Result+=" ( Pharma_MonthlySales.intYear>=" + CurrentPeriod.FromDate.Year.ToString() + " AND Pharma_MonthlySales.byteMonth >= " + CurrentPeriod.FromDate.Month.ToString() + " AND Pharma_MonthlySales.intYear<=" + CurrentPeriod.ToDate.Year.ToString() + " AND Pharma_MonthlySales.byteMonth <= " + CurrentPeriod.ToDate.Month.ToString() + " ) ";
            }

            if (!string.IsNullOrEmpty(Result))
                Result = " AND ( " + Result  + " ) ";
            return Result;
        }


    }
}
